package com.yuehuanghun.admin.core.controller.system;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yuehuanghun.admin.core.config.UserConstants;
import com.yuehuanghun.admin.core.config.util.UserUtils;
import com.yuehuanghun.admin.core.config.util.poi.ExcelUtil;
import com.yuehuanghun.admin.core.domain.SysRole;
import com.yuehuanghun.admin.core.service.ISysRoleService;
import com.yuehuanghun.framework.annotation.Log;
import com.yuehuanghun.framework.annotation.Log.BusinessType;
import com.yuehuanghun.framework.domain.PageDTO;
import com.yuehuanghun.framework.exception.ValidateException;
import com.yuehuanghun.framework.web.BaseResource;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;

/**
 * 角色信息
 * 
 * @author yadmin
 */
@RestController
@RequestMapping("/system/role")
@SaCheckLogin
public class SysRoleController extends BaseResource {
	@Autowired
	private ISysRoleService roleService;

	@GetMapping("/list")
	@SaCheckPermission("system:role:list")
	public PageDTO<SysRole> list(SysRole role) {
		startPage();
		List<SysRole> list = roleService.getRoleList(role);
		return toPage(list);
	}

	@Log(title = "角色管理", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	@SaCheckPermission("system:role:export")
	public String export(SysRole role) {
		List<SysRole> list = roleService.getRoleList(role);
		ExcelUtil<SysRole> util = new ExcelUtil<>(SysRole.class);
		return util.exportExcel(list, "角色数据");
	}

	/**
	 * 根据角色编号获取详细信息
	 */
	@GetMapping(value = "/{roleId}")
	@SaCheckPermission("system:role:query")
	public SysRole getInfo(@PathVariable Long roleId) {
		return roleService.getById(roleId).orElse(null);
	}

	/**
	 * 新增角色
	 */
	@Log(title = "角色管理", businessType = BusinessType.INSERT)
	@PostMapping
	@SaCheckPermission("system:role:add")
	public void add(@Validated @RequestBody SysRole role) {
		if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleNameUnique(role))) {
			throw new ValidateException("新增角色'" + role.getRoleName() + "'失败，角色名称已存在");
		} else if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleKeyUnique(role))) {
			throw new ValidateException("新增角色'" + role.getRoleName() + "'失败，角色权限已存在");
		}
		role.setDelFlag("0");
		role.setDataScope("1");
		roleService.insertRole(role);

	}

	/**
	 * 修改保存角色
	 */
	@Log(title = "角色管理", businessType = BusinessType.UPDATE)
	@PutMapping
	@SaCheckPermission("system:role:edit")
	public void edit(@Validated @RequestBody SysRole role) {
		roleService.checkRoleAllowed(role);
		if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleNameUnique(role))) {
			throw new ValidateException("修改角色'" + role.getRoleName() + "'失败，角色名称已存在");
		} else if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleKeyUnique(role))) {
			throw new ValidateException("修改角色'" + role.getRoleName() + "'失败，角色权限已存在");
		}
		roleService.updateRole(role);
	}

	/**
	 * 修改保存数据权限
	 */
	@Log(title = "角色管理", businessType = BusinessType.UPDATE)
	@PutMapping("/dataScope")
	@SaCheckPermission("system:role:edit")
	public void dataScope(@RequestBody SysRole role) {
		roleService.checkRoleAllowed(role);
		roleService.authDataScope(role);
	}

	/**
	 * 状态修改
	 */
	@Log(title = "角色管理", businessType = BusinessType.UPDATE)
	@PutMapping("/changeStatus")
	@SaCheckPermission("system:role:edit")
	public void changeStatus(@RequestBody SysRole role) {
		roleService.checkRoleAllowed(role);
		role.setUpdateBy(UserUtils.getCurrentUserName());
		roleService.updateRoleStatus(role);
	}

	/**
	 * 删除角色
	 */
	@Log(title = "角色管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{roleIds}")
	@SaCheckPermission("system:role:remove")
	public void remove(@PathVariable Long[] roleIds) {
		roleService.deleteRoleByIds(roleIds);
	}

	/**
	 * 获取角色选择框列表
	 */
	@GetMapping("/optionselect")
	@SaCheckPermission("system:role:query")
	public List<SysRole> optionselect() {
		return roleService.getRoleAll();
	}
}
